<?php


namespace App\Services;


use App\Admin\Actions\Tools\PatentFeeEndBatchAction;
use App\Extensions\Column\CheckRelationFilter;
use App\Extensions\Column\InputRelationFilter;
use App\Extensions\Grid\Exporters\CsvExporter;
use Carbon\Carbon;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Grid\Tools\Selector;

class PatentFeeService
{
    public function gridColumn(\Encore\Admin\Grid $grid)
    {
        $_scope_ = request()->get('_scope_');
        $grid->model()->with(['patent'])->when($_scope_ == 'complete', function ($query) {
            $query->where('state', 1);
        }, function ($query) {
            $query->where('state', 0);
        })->whereDate('last_time', '<=', Carbon::now()->addYears()->toDateString());
        $grid->column('patent_sn', __('专利号'))->filter('like');
        $grid->column('patent.patent_name', __('专利名称'))->addHeader(new InputRelationFilter('like', 'patent', 'patent_name'));
        $grid->column('name', __('缴费名称'));
        $grid->column('amount', __('缴费金额'))->totalRow(function ($amount) {
            return "<span class='text-danger text-bold'>共计:<i class='fa fa-yen'></i> {$amount} 元</span>";
        });
        $grid->column('last_day', __('剩余天数'))->display(function () {
            $day = Carbon::now()->diffInDays($this->last_time, false);
            if ($day < 30) {
                return '<b style="color: red">' . $day . '</b>';
            }
            return $day;
        });
        $grid->column('last_time', __('缴费截止日'));
        $grid->column('patent.patent_type_id', __('专利类型'))->using(patent_type_select_option())
            ->addHeader(new CheckRelationFilter(patent_type_select_option(), 'patent', 'patent_type_id'));
        $grid->column('patent.apply_date', __('申请日期'));
        $grid->column('state', __('缴费状态'))->using(['待缴费','已缴费'])->label(['warning','success']);
        $grid->batchActions(function (BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->tools(function(Tools $tools){
            $tools->append(new PatentFeeEndBatchAction());
        });
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->scope('complete', '已缴费官方未更新');
        });
        $grid->selector(function (Selector $selector) {
            $selector->selectOne('last_time', '缴费截止日', [1 => '一个月内', 2 => '三个月内容', 3 => '半年内', 4 => '超期'], function ($query, $value) {
                switch ($value) {
                    case 1:
                        $time = Carbon::now()->addMonths();
                        $query->where('last_time', '<=', $time);
                        break;
                    case 2:
                        $time = Carbon::now()->addMonths(3);
                        $query->where('last_time', '<=', $time);
                        break;
                    case 3:
                        $time = Carbon::now()->addMonths(6);
                        $query->where('last_time', '<=', $time);
                        break;
                    case 4:
                        $query->where('last_time', '<', Carbon::now()->toDateString());
                        break;
                }
            });
        });
        //$grid->exporter(new PatentFeeExporter());
        $grid->exporter(new CsvExporter());
        $grid->export(function (CsvExporter $exporter) {
            $exporter->filename(date('Ymd') . '待缴年费');
            $exporter->except(['last_day']);
            $exporter->column('patent_sn', function ($value, $original) {
                return $original . "\t";
            });
            $exporter->column('patent.apply_date', function ($value, $original) {
                return $original . "\t";
            });
            $exporter->column('last_time', function ($value, $original) {
                return $original . "\t";
            });
        });
        return $grid;
    }
}
